********************************************************************************
** 	TITLE:		ON14_results                                                  ** 	
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       ON14_results                                                  **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* 1. Download the "Valid Votes Cast for Each Candidate" file from this link: https://results.elections.on.ca/api/report-groups/2/report-outputs/488/csv
* 2. Save the file to the CSV format with the following name: "ON14_results.csv"

* Import the dataset

import delimited "ON14_results.csv", clear

* Keep only general election

drop if isgeneralelection == 0

* Rename and (re)create variables

rename electoraldistrictnumber districtnumber14
rename electoraldistrictnameenglish districtname14
rename politicalinterestcode partyname14
rename totalvalidballotscast numberofvotes14
gen numberofvotespercent14 = percentoftotalvalidballotscast*100

* Generate dichotomous variable for each party

gen lib14 = 1 if partyname14 == "OLP"
replace lib14 = 0 if lib14!=1

gen con14 = 1 if partyname14 == "PCP"
replace con14 = 0 if con14!=1

gen ndp14 = 1 if partyname14 == "NDP"
replace ndp14 = 0 if ndp14!=1

gen green14 = 1 if partyname14 == "GPO"
replace green14 = 0 if green14!=1

* Generate party variable

gen party14 = 1 if lib14 == 1
replace party14 = 2 if con14 == 1
replace party14 = 3 if ndp14 == 1
replace party14 = 4 if green14 == 1
replace party14 = 88 if party14 == . 

* Generate rank of parties

gsort districtname14 -numberofvotes14
bysort districtname14 : gen rank14 = _n

gen first14 = party14 if rank14 == 1
gen second14 = party14 if rank14 == 2
gen third14 = party14 if rank14 == 3

bysort districtname14 : gen winner14 = first14[1]
bysort districtname14 : gen runnerup14 = second14[2]
bysort districtname14 : gen thirdplace14 = third14[3]

* Generate margins of victory

bysort districtname14 : gen margin14 = numberofvotespercent14[1] - numberofvotespercent14[2]

* Generate effective number of electoral parties

gen sqrvotepr14 = (numberofvotespercent14/100)^2

bysort districtname14 : egen sumsqrvotepr14 = sum(sqrvotepr14)

gen enep14 = 1/sumsqrvotepr14

* Labelling parties

label define party 1 "Liberal Party" 2 "Progressive Conservative Party" 3 "New Democratic Party" 4 "Green Party" 88 "Other"
label values winner14 runnerup14 thirdplace14 party

* Selecting, ordering, and sorting variables

keep districtnumber14 districtname14 party14 rank14 numberofvotes14 numberofvotespercent14 margin14 enep14 winner14 runnerup14 thirdplace14

order districtnumber14 districtname14 party14 rank14 numberofvotes14 numberofvotespercent14 margin14 enep14 winner14 runnerup14 thirdplace14

sort districtnumber14 rank14

drop if rank14 > 3

* Create election variable

gen election14 = "2014 Ontario general election"

* Saving file

save ON14_results, replace

* Drop duplicates

duplicates tag districtnumber14, gen(dup)
duplicates drop districtnumber14, force
drop dup

* Merge with ON14_results.dta and generate reelection variable

merge 1:1 _n using ON11_results.dta

* Generate variable for incumbent party reelection

gen reelected14 = 1 if winner14 == winner11
replace reelected14 = 0 if reelected14!=1

* Saving file

save ON1114_results, replace